<?php
$flag=$_REQUEST['flag'];
$cg=$_REQUEST['cgName'];

$link=new mysqli('127.0.0.1','root','root','books_manage_system','3306');
if($link->connect_error){
    die('连接失败'.$link->connect_error);
}else{
    //连接成功，设置编码格式
    mysqli_set_charset($link,'utf8');
    $link->query('SET NAMES UTF8');

    //存储数据
    $result=array();
    $result['data']=array();


    //是否是通过图书管理请求
    if($flag=='books'){
        //是
        $sql="select * from books where dstatus=1";
        $re=$link->query($sql);
        $num=mysqli_num_rows($re);
        if($num>0){
            // 遍历数据  存储在$reuslt
            while($rows = $re->fetch_assoc()) {
                $result['status']='success';
                array_push($result['data'],$rows);
            }
        }else{
            $result['status']='fail';
        }
        mysqli_free_result($re);
    }else{
        //否
        $sql1="select * from books where category='$cg' and dstatus=1";
        $re1=$link->query($sql1);
        $num1=mysqli_num_rows($re1);
        if($num1>0){
            $sql="select * from books where category='$cg' and dstatus=1 limit 0,5";
            $re=$link->query($sql);
            $num=mysqli_num_rows($re);
            if($num>0){
                // 遍历数据  存储在$reuslt
                while($rows = $re->fetch_assoc()) {
                    $result['status']='success';
                    array_push($result['data'],$rows);
                }
            }else{
                $result['status']='fail';
            }
            mysqli_free_result($re); //释放结果集
        }else{
            $result['status']='fail';
        }
    mysqli_free_result($re1);
    }
    //关闭数据库
    $link->close();
    die(json_encode($result));
    }
?>